<script setup lang="ts">
  import moment from 'moment'
  import { inject } from 'vue'
  import { useStore } from 'vuex'
  import { onLaunch } from '@dcloudio/uni-app'
  import { SET_USER_INFO, SET_TGT_TOKEN, SET_TOKEN } from './store/types'
  const store = useStore()
  const $Prefix = inject('$Prefix')
  //首次进入加载应用
  onLaunch(async (options) => {
    initStore()
    // 加载字体
    // loadFontFaceFromLocal()
    // #ifdef MP-WEIXIN
    await fourceUpdate()
    uni.showShareMenu({
      menus: ['shareAppMessage', 'shareTimeline'],
    })
    // #endif
    launchTime()
  })

  // 加载字体
  function loadFontFaceFromLocal() {
    uni.loadFontFace({
      global:true,
      family:'SourceHanSans',
      source:`url('${$Prefix}/public/fonts/SourceHanSansCN-Regular.otf')`
    })
  }

  // 获取小程序更新机制兼容
  async function fourceUpdate() {
    if (uni.canIUse('getUpdateManager')) {
      try {
        const { hasUpdate } = await updateManagerAsync('onCheckForUpdate')
        if (hasUpdate) {
          await updateManagerAsync('onUpdateReady')
          uni.showModal({
            title: '更新提示',
            content: '新版本已经准备好，是否重启应用？',
            success: function (res) {
              if (res.confirm) {
                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                updateManagerAsync('applyUpdate')
              }
            },
          })
          await updateManagerAsync('onUpdateFailed')
          uni.showModal({
            title: '已经有新版本了',
            content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开',
          })
        }
      } catch (e) {
      }
      return true
    }
    uni.showModal({
      title: '提示',
      content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。',
    })
    return true
  }

  function updateManagerAsync(key: keyof UniApp.UpdateManager) {
    const updateManager = uni.getUpdateManager()
    return new Promise<any>(function (resolve, reject) {
      const fn = updateManager[key]
      fn((res) => resolve(res))
    })
  }

  // 初始化Storeage用户信息
  function initStore() {
    const userInfo = uni.getStorageSync('userInfo') || null
    const token =uni.getStorageSync('token') || null
    const tgt = uni.getStorageSync('threePartLoginData')?.tgt || null
    store.commit(`user/${SET_USER_INFO}`, userInfo)
    store.commit(`user/${SET_TGT_TOKEN}`, tgt)
    store.commit(`user/${SET_TOKEN}`, token)
  }

  // 记录加载次数 每日清除
  function launchTime() {
    const timeCounter = {
      date: moment().format('YYYY/MM/DD'),
      count: 0,
    }
    let history = uni.getStorageSync('launchTimeCounter') || timeCounter

    // 缓存时间与当前时间不在同一天 一天一次
    if (history.date !== timeCounter.date) {
      // 重置日打开次数
      history = timeCounter
    }

    history.date = moment().format('YYYY/MM/DD')
    history.count++
    uni.setStorageSync('launchTimeCounter', history)
  }
</script>

<style lang="scss">
  @import "uview-plus/index.scss";
  /*
		全局公共样式和字体图标
	*/
  view,
  scroll-view,
  swiper,
  swiper-item,
  cover-view,
  cover-image,
  icon,
  text,
  rich-text,
  progress,
  button,
  checkbox,
  form,
  input,
  label,
  radio,
  slider,
  switch,
  textarea,
  navigator,
  audio,
  camera,
  image,
  video {
    box-sizing: border-box;
    font-family: 'SourceHanSans';
    font-variation-settings: "opsz" auto;
    font-feature-settings: "kern" on;
  }
  page{
    background: #FFFFFF;
    font-family: 'SourceHanSans';
  }
  /*边框*/
  .b-b:after,
  .b-t:after {
    position: absolute;
    z-index: 3;
    left: 0;
    right: 0;
    height: 0;
    content: '';
    transform: scaleY(0.5);
    // border-bottom: 1rpx solid $border-color-base;
  }
  .b-b:after {
    bottom: 0;
  }

  .b-t:after {
    top: 0;
  }
  /* button样式改写 */
  uni-button,
  button {
    height: 80upx;
    line-height: 80upx;
    font-size: $font-lg + 2upx;
    font-weight: normal;

    &.no-border:before,
    &.no-border:after {
      border: 0;
    }
  }
  uni-button[type='default'],
  button[type='default'] {
    color: $font-color-dark;
  }
  /* input 样式 */
  .input-placeholder {
    color: #999999;
  }

  .placeholder {
    color: #999999;
  }

  button {
    padding-left: 0px;
    padding-right: 0px;
    margin-left: 0;
    margin-right: 0;
  }
  .u-empty__text{
    margin-top: 5rpx !important;
  }
</style>
